//
//  HomeHeaderView.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/19.
//  Copyright © 2018年 guoshikeji. All rights reserved.
//

#import "HomeHeaderView.h"

#import "HomeHeaderMessageView.h"

//data
#import "BCHomeDataResponse.h"

@interface HomeHeaderView ()

@property (nonatomic, strong) UIView *stretchView;

@property (nonatomic, strong) UIImageView *homeBackgroundIV;
@property (nonatomic, strong) UIImageView *homeHeadImageView;

@property (nonatomic, strong) HomeHeaderMessageView *messageView;

/**
 *fillBackView
 */
@property (nonatomic, strong) UIView *fillBackView;


@end

@implementation HomeHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews {
    self.stretchView = [UIView new];
    self.stretchView.backgroundColor = [UIColor darkMainColor];
    [self addSubview:self.stretchView];
    [self.stretchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self.mas_top);
        make.height.mas_equalTo(CGFLOAT_MAX);
    }];
    
    //
    self.homeBackgroundIV = [UIImageView new];
    [self addSubview:self.homeBackgroundIV];
    [self.homeBackgroundIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).offset(Height_StatusBar-20.0);
        make.left.right.mas_equalTo(self);
    }];
    //
    self.homeBackgroundIV.image = [LOAD_IMAGE(@"home_head_background") imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    self.homeBackgroundIV.tintColor = [UIColor darkMainColor];
    //
    UIView *fillBackView = [UIView new];
    fillBackView.backgroundColor = [UIColor darkMainColor];
    [self addSubview:fillBackView];
    [fillBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self.homeBackgroundIV.mas_top);
    }];
    self.fillBackView = fillBackView;
    //
    self.homeHeadImageView = [UIImageView new];
    [self.homeBackgroundIV addSubview:self.homeHeadImageView];
    [self.homeHeadImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.homeBackgroundIV).offset(-10.0);
        make.top.mas_equalTo(self.homeBackgroundIV).offset(49.0);
    }];
    self.homeHeadImageView.image = LOAD_IMAGE(@"home_head_img");
    
    //
    self.messageView = [HomeHeaderMessageView new];
    [self addSubview:self.messageView];
    [self.messageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(11.0);
        make.right.mas_equalTo(self).offset(-11.0);
        make.height.mas_equalTo([HomeHeaderMessageView defaultHeight]);
        make.bottom.mas_equalTo(self).offset(-8.0);
    }];
    [self.messageView.messageContent addTarget:self action:@selector(messageViewContentDidClickedAction) forControlEvents:UIControlEventTouchUpInside];
    
    //
    [self displaySubviews:false];
}

- (void)layoutSubviewsWithTableViewContentOffset:(CGPoint)contentOffset {
//    CGFloat stretchViewHeight = -contentOffset.y;
//    stretchViewHeight = MAX(0, stretchViewHeight);
//    [self.stretchView mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.height.mas_equalTo(stretchViewHeight);
//    }];
}



- (void)displaySubviews:(BOOL)display {
    self.stretchView.alpha = self.fillBackView.alpha = self.homeBackgroundIV.alpha = self.messageView.alpha = display?1:0;
    
}


- (void)setHomeData:(BCHomeData *)homeData {
    _homeData = homeData;
    if (homeData) {
        [self displaySubviews:true];
        if (homeData.message.count) {
            self.messageView.alpha = 1;
            self.messageView.messages = homeData.message;
        }else {
            self.messageView.alpha = 0;
            self.messageView.messages = nil;
        }
    }else {
         [self displaySubviews:false];
    }
}


#pragma mark - messageViewContentDidClickedAction
- (void)messageViewContentDidClickedAction {
    BCHomeDataMessage *message = self.messageView.messageContent.message;
    if (message.url.length) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(homeHeaderViewMessageDidClicked:)]) {
            [self.delegate homeHeaderViewMessageDidClicked:message];
        }
    }
}

+ (CGFloat)defaultHeight {
    return Height_StatusBar+156.0;
}

@end
